﻿using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace lwzlz.Web.Html
{
    public static class MenuButtonHelper
    {
        /// <summary>
        /// 用户权限可见按钮
        /// </summary>
        /// <param name="type">按钮位置类型</param>
        /// <param name="isHasRow">是否有行按钮</param>
        /// <param name="routeValues">路由值</param>
        /// <param name="config">按钮显示配置</param>
        /// <param name="rowFunc">行方法委托</param>
        /// <returns></returns>
        public static MvcHtmlString SysMenuButton(this HtmlHelper helper, MenuPosType type, out bool isHasRow,
            IDictionary<string, object> routeValues = null, SysButtonConfig config = null, Func<IEnumerable<AuthorityMenuDto>, string> rowFunc = null)
        {
            string value = "";
            isHasRow = false;
            IEnumerable<AuthorityMenuDto> buttons = null;
            if (routeValues == null)
            {
                // 获取当前视图Url路由数据
                routeValues = helper.ViewContext.RouteData.Values;
            }
            // 获取指定页按钮
            buttons = new AuthorityService().GetAuthorityButtons(routeValues);
            if (buttons != null && buttons.Count() > 0)
            {
                if (config == null)
                {
                    config = new SysButtonConfig();
                }

                switch (type)
                {
                    case MenuPosType.列:
                        value = ColButton(buttons, config);
                        break;
                    case MenuPosType.行:
                        value = RowButton(buttons, out isHasRow, rowFunc, config);
                        break;
                    case MenuPosType.行列:
                        value = ColRowButton(buttons, out isHasRow, rowFunc, config);
                        break;
                    case MenuPosType.隐藏:
                        break;
                    default:
                        break;
                }
            }
            return new MvcHtmlString(value);
        }

        /// <summary>
        /// 列按钮
        /// </summary>
        /// <param name="buttons"></param>
        /// <param name="config">按钮显示配置</param>
        /// <returns></returns>
        private static string ColButton(IEnumerable<AuthorityMenuDto> buttons, SysButtonConfig config)
        {
            StringBuilder strbHtml = new StringBuilder();
            int colValue = (int)MenuPosType.列,
                crValue = (int)MenuPosType.行列;
            // 获取当前页按钮中所有列按钮
            var colButtons = buttons.Where(p => p.BtnPosType == colValue || p.BtnPosType == crValue);
            if (colButtons != null && colButtons.Count() > 0)
            {
                strbHtml.AppendLine("<div class=\"layui-btn-group\">");
                foreach (var item in colButtons)
                {
                    strbHtml.AppendLine(string.Format(
                        "<button type=\"button\" title=\"{0}\" class=\"layui-btn {1}\" data-type=\"{2}\"><i class=\"layui-icon\">{3}</i>{4}</button>",
                        item.MenuName, item.MenuCss, string.IsNullOrWhiteSpace(item.MenuAction) ? item.MenuMethod : item.MenuAction,
                        config.IsShowColIcon ? item.MenuIcon : "", config.IsShowColText ? item.MenuName : ""));
                }
                strbHtml.AppendLine("</div>");
            }

            return strbHtml.ToString();
        }

        /// <summary>
        /// 行按钮
        /// </summary>
        /// <param name="buttons"></param>
        /// <param name="isHasRow"></param>
        /// <param name="func">行方法委托</param>
        /// <param name="config">按钮显示配置</param>
        /// <returns></returns>
        private static string RowButton(IEnumerable<AuthorityMenuDto> buttons, out bool isHasRow,
            Func<IEnumerable<AuthorityMenuDto>, string> func, SysButtonConfig config)
        {
            StringBuilder strbHtml = new StringBuilder();
            isHasRow = false;
            int rowValue = (int)MenuPosType.行,
                crValue = (int)MenuPosType.行列;
            // 获取当前页按钮中所有行按钮
            var rowButtons = buttons.Where(p => p.BtnPosType == rowValue || p.BtnPosType == crValue);
            if (rowButtons != null && rowButtons.Count() > 0)
            {
                isHasRow = true;

                if (func == null) //委托为空则按默认输出
                {
                    strbHtml.AppendLine("<div class=\"layui-btn-group\">");
                    foreach (var item in rowButtons)
                    {
                        strbHtml.AppendLine(string.Format(
                            "<button type=\"button\" title=\"{0}\" class=\"layui-btn layui-btn-sm {1}\" lay-event=\"{2}\"><i class=\"layui-icon\">{3}</i>{4}</button>",
                            item.MenuName, item.MenuCss, string.IsNullOrWhiteSpace(item.MenuAction) ? item.MenuMethod : item.MenuAction,
                            config.IsShowRowIcon ? item.MenuIcon : "", config.IsShowRowText ? item.MenuName : ""));
                    }
                    strbHtml.AppendLine("</div>");
                }
                else
                {
                    strbHtml.Append(func(rowButtons));
                }
            }

            return strbHtml.ToString();
        }

        /// <summary>
        /// 行列按钮
        /// </summary>
        /// <param name="buttons"></param>
        /// <param name="isHasRow"></param>
        /// <param name="rowFunc">行方法委托</param>
        /// <param name="config">按钮显示配置</param>
        /// <returns></returns>
        private static string ColRowButton(IEnumerable<AuthorityMenuDto> buttons, out bool isHasRow,
            Func<IEnumerable<AuthorityMenuDto>, string> rowFunc, SysButtonConfig config)
        {
            StringBuilder strbHtml = new StringBuilder();
            isHasRow = false;

            string col = ColButton(buttons, config);
            string row = RowButton(buttons, out isHasRow, rowFunc, config);
            strbHtml.AppendLine(col);
            if (isHasRow)
            {
                strbHtml.AppendLine("<script type=\"text/html\" id=\"barOperation\">");
                strbHtml.AppendLine(row);
                strbHtml.AppendLine("</script>");
            }

            return strbHtml.ToString();
        }

    }

    /// <summary>
    /// 按钮显示配置
    /// </summary>
    public class SysButtonConfig
    {
        /// <summary>
        /// 是否显示行按键文字
        /// </summary>
        public bool IsShowRowText { get; set; }
        /// <summary>
        /// 是否显示行按键图标
        /// </summary>
        public bool IsShowRowIcon { get; set; }
        /// <summary>
        /// 是否显示列按键文字
        /// </summary>
        public bool IsShowColText { get; set; }
        /// <summary>
        /// 是否显示列按键图标
        /// </summary>
        public bool IsShowColIcon { get; set; }

        public SysButtonConfig()
        {
            // 默认配置
            IsShowRowText = false;
            IsShowRowIcon = true;
            IsShowColText = true;
            IsShowColIcon = true;
        }
    }

}